﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qkview.Models;

namespace qkview.Controllers
{
    [Authorize(Roles="admin")]  
    public class AdminUserController : Controller
    {
        private QKVEntities db = new QKVEntities();

        //
        // GET: /AdminUser/

        public ActionResult Index()
        {
            string sql = "SELECT TOP 1000 up.[UserId]" +
"      ,up.[Name]" +
"      ,up.[Email]" +
"      ,up.[UserName]" +
"      ,up.[IsBlocked]" +
"      ,us.TotalBoards " +
"      ,us.TotalNotes " +
"      ,us.TotalSection " +
"      ,us.AdvSecDescription " +
"      ,wpm.CreateDate      " +
"      ,wpm.IsConfirmed" +
"      ,wpo.Provider      " +
"  FROM [UserProfile] up" +
"  left join UserSetting us on up.UserId = us.UserID " +
"  left join webpages_Membership wpm on up.UserId = wpm.UserId " +
"  left join webpages_OAuthMembership wpo on up.UserId = wpo.UserId " +
"  order by wpm.CreateDate desc";

            List<FullUserInfo> ret = db.Database.SqlQuery<FullUserInfo>(sql).ToList();
            return View(ret);
        }

        //
        // GET: /AdminUser/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminUser/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminUser/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminUser/Edit/5

        public ActionResult Edit(int id)
        {
            string sql = "SELECT up.[UserId]" +
"      ,up.[Name]" +
"      ,up.[Email]" +
"      ,up.[UserName]" +
"      ,up.[IsBlocked]" +
"      ,us.TotalBoards " +
"      ,us.TotalNotes " +
"      ,us.TotalSection " +
"      ,us.AdvSecDescription " +
"      ,wpm.CreateDate      " +
"      ,wpm.IsConfirmed" +
"      ,wpo.Provider      " +
"  FROM [UserProfile] up" +
"  left join UserSetting us on up.UserId = us.UserID " +
"  left join webpages_Membership wpm on up.UserId = wpm.UserId " +
"  left join webpages_OAuthMembership wpo on up.UserId = wpo.UserId where up.UserID=@userID " +
"  order by wpm.CreateDate desc";

            FullUserInfo ret = db.Database.SqlQuery<FullUserInfo>(sql, new SqlParameter("userID", id)).FirstOrDefault();
            return View(ret);
        }

        //
        // POST: /AdminUser/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FullUserInfo obj)
        {
          
                if (ModelState.IsValid)
                {
                    var up = db.UserProfiles.FirstOrDefault(x => x.UserId == id);
                    up.Name = obj.Name;
                    up.Email = obj.Email;
                    up.UserName = obj.UserName;
                    up.IsBlocked = obj.IsBlocked;
                    UserSetting us;
                    if (up.UserSettings == null || up.UserSettings.Count() == 0)
                    {
                        us = new UserSetting()
                        {
                            TotalBoards = obj.TotalBoards ?? 0,
                            TotalNotes = obj.TotalNotes ?? 0,
                            TotalSection = obj.TotalSection ?? 0,
                            AdvSecDescription = obj.AdvSecDescription ?? false,
                            LastModified = DateTime.UtcNow
                        };
                        up.UserSettings.Add(us);
                    }
                    else
                    {
                        us = up.UserSettings.FirstOrDefault();
                        us.TotalBoards = obj.TotalBoards ?? 0;
                        us.TotalNotes = obj.TotalNotes ?? 0;
                        us.TotalSection = obj.TotalSection ?? 0;
                        us.AdvSecDescription = obj.AdvSecDescription ?? false;
                        us.LastModified = DateTime.UtcNow; 
                    }
                    var mem = db.webpages_Membership.FirstOrDefault(x => x.UserId == id);
                    mem.IsConfirmed = obj.IsConfirmed ?? false;

                     db.SaveChanges(); 

                     return RedirectToAction("Index");
                }             
           
            return View(obj);
        }

        //
        // GET: /AdminUser/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminUser/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Board()
        {
            return View(db.Boards.OrderByDescending(x => x.CreatedDate).ToList());
        }

        public ActionResult BlockBoard(int id)
        {
            var obj = db.Boards.FirstOrDefault(x => x.BoardID == id);
            if (obj != null) {
                obj.IsBlocked =(obj.IsBlocked.HasValue && obj.IsBlocked.Value) ? false : true;
                db.SaveChanges();
            }
            return RedirectToAction("Board"); 
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
